﻿package com._public._net
{
	import com._public._utils.strUtil;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.StyleSheet;
	import flash.text.TextFormat;
	
	/**
	 * ...
	 * @author WZH(shch8.com)
	 */
	public class GetCSS extends EventDispatcher
	{
		private static var Instance:GetCSS = new GetCSS;
		public static function getInstance():GetCSS {
			return Instance;
		}
		private var sheet:StyleSheet = new StyleSheet;
		private var loader:URLLoader = new URLLoader();
		private var _textFormat:TextFormat;
		private var _cssPath:String = "";
		public function set cssPath(value:String):void//"styles/style.css"
		{
			_cssPath = value;
			 var req:URLRequest = new URLRequest(value);
            loader.load(req);
            loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
            loader.addEventListener(Event.COMPLETE, loaderCompleteHandler);
		}
		public function get cssPath():String { return _cssPath; }
		public function get styleSheet():StyleSheet {
			return sheet;
			}
		/**
		 * 全局样式
		 */
		public function get textFormat():TextFormat {
			return _textFormat;
			}
		
		 private function errorHandler(e:IOErrorEvent):void {
           throw("CSS样式包路径不对，请在添加到舞台之前设置正确的路径！错误地址:"+_cssPath);
        }

        private function loaderCompleteHandler(event:Event):void {
			sheet.parseCSS(loader.data);
			this.dispatchEvent(new Event(Event.COMPLETE));
			_textFormat= new TextFormat();
            var style:Object = sheet.getStyle("globDDalTextFormat");
            strUtil.textFormat=_textFormat = sheet.transform(style);
        }
	}
	
}